package com.library.utils;/*
 *@author lzg
 *@data 2020/11/17
 */

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.sun.org.apache.bcel.internal.generic.NEW;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCTools {
    private static DataSource dataSource;
    static {
        dataSource = new ComboPooledDataSource("testc3p0");
    }
    //获取连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //释放数据库连接资源
    public static void release(Connection connection, PreparedStatement preparedStatement, ResultSet... resultSet){
        try {
            if (null != connection){
                connection.close();
            }
            if (null != preparedStatement){
                preparedStatement.close();
            }
            if (resultSet.length!=0){
                resultSet[0].close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
